package com.huazi.www.yang.fchua.service.Imlp;

import com.huazi.www.yang.fchua.dao.BillDao;
import com.huazi.www.yang.fchua.dao.Impl.BillDaoImpl;
import com.huazi.www.yang.fchua.entity.Medicine;
import com.huazi.www.yang.fchua.entity.Prescription;
import com.huazi.www.yang.fchua.service.BillService;
import com.huazi.www.yang.fchua.uilt.Page;

import java.util.List;

public class BillServiceImpl implements BillService {
    BillDao dao = new BillDaoImpl();

    @Override
    public Page<Medicine> page(String name, int currentPage) {
        //创建分页对象
        Page<Medicine> page = new Page<>();
        //设置页大小
        page.setPageSize(5);
        //设置当前页码
        page.setCurrPageNo(currentPage);
        //查询总记录数
        page.setTotalCount(dao.totalCount(name));
        //计算起始下标
        int start = (currentPage - 1) * page.getPageSize();
        //查询分页数据
        page.setNewsList(dao.list(name, start, page.getPageSize()));
        //返回分页对象
        return page;
    }

    @Override
    public List<Medicine> list(String userId, String date) {
        return dao.list(userId, date);
    }

    //得到药单的总价格
    @Override
    public double money(List<Medicine> list) {
        //总价格
        double money = 0;
        //循环计算价格
        for (int i = 0; i < list.size(); i++) {
            //计算总价格
            money += list.get(i).getPrice() * list.get(i).getSum();
        }

        return money;
    }

    @Override
    public String drugs(List<Medicine> list) {
        //药品拼接
        String drugs = "";
        //循环拼接药品
        for (int i = 0; i < list.size(); i++) {
            //药品拼接
            drugs += list.get(i).getMedicineName() + "*" + list.get(i).getSum();
            if (i < list.size() - 1) {
                drugs += "|";
            }
        }
        return drugs;
    }

    @Override
    public void update(List<Medicine> list) {
        for (int i = 0; i < list.size(); i++) {
            dao.updates(list.get(i).getId());
        }
    }

    @Override
    public boolean pdMoney(String userID, double drugMoney) {
        //查询用户余额
        double money = dao.selectUserMoney(Integer.parseInt(userID));
        //判断用户余额是否能支付药费
        if (money >= drugMoney) {
            //扣除用户余额
            dao.updateUserMoney(drugMoney, Integer.parseInt(userID));
            return true;
        }
        return false;
    }

    /**
     * 分页查询药缴费单
     *
     * @param name
     * @param currentPage
     * @return
     */
    @Override
    public Page<Medicine> yaoSelect(String name, Integer currentPage) {
        Page<Medicine> page = new Page<>();
        page.setCurrPageNo(currentPage);
        page.setPageSize(5);
        page.setTotalCount(dao.totalCount());
        Integer start = (page.getCurrPageNo() - 1) * page.getPageSize();
        List<Medicine> list = dao.yaoSelect(name, start, page.getPageSize());
        page.setNewsList(list);
        return page;
    }

    /**
     * 药缴费单插入
     *
     * @param pre
     * @return
     */
    @Override
    public boolean insert(Prescription pre) {
        int count = dao.insert(pre);
        return count == 1;
    }

    /**
     * 根据药drugname查询价格
     *
     * @return
     */
    @Override
    public double selectPrice(String drugname) {
        double count = dao.selectPrice(drugname);
        return count;
    }

    /**
     * 修改药单的状态
     *
     * @param userId
     */
    @Override
    public void update(Integer userId) {
        dao.update(userId);
    }

    /**
     * 查询用户余额
     *
     * @param id  用户id
     * @param sum 药的数量
     * @return
     */
    @Override
    public boolean selectUserMoney(Integer id, Integer sum, double count) {
        double money = dao.selectUserMoney(id); //余额
        double total = sum * count;
        if (money > total) {
            dao.updateUserMoney(total, id);
            return true;
        }
        return false;
    }
}
